package jjn.round2;

/**
 * @author Jiang Jining
 * @since 2023-05-08 23:06
 */
public class Offer58_1_ReverseWord {
    public String reverseWords(String s) {
        s = s.trim();
        StringBuilder stringBuilder = new StringBuilder();
        int right = s.length() - 1;
        int left = s.length() - 1;
        while (left >= 0) {
            while (left >= 0 && s.charAt(left) != ' ') {
                left--;
            }
            stringBuilder.append(s.substring(left + 1, right + 1) + " ");
            while (left >= 0 && s.charAt(left) == ' ') {
                left--;
            }
            right = left;
        }
        return stringBuilder.toString().trim();
    }
}
